#import "AppDelegate.h"
#import <Flutter/Flutter.h>
#import "GeneratedPluginRegistrant.h"
#import "NativeViewController.h"
#import "MainViewControllerDemo.h"


@interface AppDelegate()
	@property (nonatomic, strong) UINavigationController *navigationController;
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  // 将flutter view作为根视图
  FlutterViewController *controller = (FlutterViewController*)self.window.rootViewController;
  // set up navigation stack
  self.navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  self.window.rootViewController = self.navigationController;
  [self.navigationController setNavigationBarHidden:NO animated:YES];
  [self.window makeKeyAndVisible];
  // flutter 通信接口
  FlutterMethodChannel* ecgChannel = [FlutterMethodChannel
											methodChannelWithName:@"plugin_ecg"
											binaryMessenger:controller];
  [ecgChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
    NSLog(@"%@", call.method);
    // 接收从flutter传递过来的参数
    NSLog(@"%@", call.arguments[@"ecg_title"]);
    if ([@"goToNativePage" isEqualToString:call.method]) {
        // NSString * storyboardName = @"Main";
        // UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
        // UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"NativeViewController"];
        // vc.navigationItem.title = call.arguments[@"ecg_title"];
        MainViewControllerDemo *viewVc = [[MainViewControllerDemo alloc] init];
        if (@available(iOS 15.0, *)) {
          UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
          [appearance configureWithOpaqueBackground];
          appearance.backgroundColor = NAVIGATION_BAR_BG_COLOR;
          NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
          [attributes setValue:UIColor.whiteColor forKey:NSForegroundColorAttributeName];
          appearance.titleTextAttributes = attributes;

          [UINavigationBar appearance].standardAppearance = appearance;
          [UINavigationBar appearance].scrollEdgeAppearance = appearance;
        }
        [self.navigationController pushViewController:viewVc animated:true];

    } else {
        result(FlutterMethodNotImplemented);
    }
  }];
  // Override point for customization after application launch.
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end
